package com.thtfpc.demo;

import com.thtfpc.ssdp.SSDPDevice;
import com.thtfpc.ssdp.SimpleServiceDeclarer;
import com.thtfpc.utility.InetUtil;
import com.thtfpc.utility.InputUtil;

import java.io.IOException;
import java.util.UUID;

public class DemoForDeclarer {

    SimpleServiceDeclarer serviceDeclarer;

    public DemoForDeclarer() {
        //创建实例
        serviceDeclarer = new SimpleServiceDeclarer();
        //发布服务
        String listenEndPoint = null;  //设定发布服务自己的局网IP
        try {
            listenEndPoint = InetUtil.getLocalHostAddress().getHostAddress();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        int port = 8086;    //发布服务监听的TCP端口，根据实际情况进行修改
        SSDPDevice device = new SSDPDevice();
        device.setTitle(InetUtil.getHostName()); //将设备名设定为本机名
        device.setId(UUID.randomUUID().toString()); //设定服务唯一ID
        device.setName("urn:schemas-upnp-org:device:MediaRenderer:1"); //设定要发布的ssdp服务类型名
        device.setServiceUrl(listenEndPoint + ":" + port); //设定接收视频数据的地址和端口
        //注册
        serviceDeclarer.registerService(device);
    }

    public void start() {
        try {
            serviceDeclarer.start();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    public void stop() {
        try {
            serviceDeclarer.stop();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    public static void main(String[] args) {
        DemoForDeclarer demo = new DemoForDeclarer();
        demo.start();
        System.out.println("type \"exit\" for quit.");
        InputUtil.quitFor("exit", new InputUtil.InputListener() {
            @Override
            public void onUserTypeString(String input) {
                System.out.println("type \"exit\" for quit.");
            }
        });
        demo.stop();
    }
}
